<?php

use Core\Common\Common;
use Core\Result\Result;
use Core\Sign\Sign;
error_reporting(E_ALL^E_NOTICE^E_WARNING);
date_default_timezone_set('Asia/Shanghai');
header("Content-type:text/html;charset=utf-8");
function loadFielEndOfPhp($class_name) {
    require_once('' . str_replace("\\","/",$class_name) . '.class.php');
}
spl_autoload_register(loadFielEndOfPhp);
$path = __DIR__ . \DIRECTORY_SEPARATOR . './' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
    require_once $path;
}
if (is_file(__DIR__ . '/vendor/autoload.php')) {
    require_once __DIR__ . '/vendor/autoload.php';
}
require_once __DIR__ ."/SendSms/src/Mail.php";
require_once __DIR__ ."/SendSms/src/Message.class.php";
require_once __DIR__ ."/SendSms/src/Sample.php";
class index
{
    function __construct(){
        $POST = Common::getPost();
        $token = $this->NginxGetAllHeaders()["Token"];
        $language = $this->NginxGetAllHeaders()["Language"];
        $POST["language"] = $language = Common::getLanguage($language);

        if(!empty($token)){
            $token = $POST["token"] = (new Sign())->decryptToken($token,$language);
        }
        //$REQUEST_URI = explode("/",$_SERVER['REQUEST_URI']);
        $TARGET=$POST["c"];
        $METHOD=$POST["a"];
        $param_arr = getopt('a:c:');
        if(!$TARGET){
            $TARGET = $param_arr["c"];
        }
        if(!$METHOD){
            $METHOD = $param_arr["a"];
        }
        if(!$TARGET){$TARGET=$_GET["c"];}
        if(!$METHOD){$METHOD=$_GET["a"];}
        Common::noLogin($METHOD,$token,$language);
        if(!empty($TARGET)){
            if (!file_exists(__DIR__ . "/App/Controller/Home/".$TARGET.".class.php")) {
                Result::error("Interface error");
            }
            $OBJECTOR = "App\Controller\Home\\".$TARGET;
            $OBJECT = new $OBJECTOR();
            if(method_exists($OBJECT,$METHOD)){
                echo $OBJECT->$METHOD($POST);
            }else{Result::error("Non-existent error");}
        }else{
            Result::error("interface error");
        }
    }
    function NginxGetAllHeaders(){//获取请求头
        $headers = [];
        foreach ($_SERVER as $name => $value){
            if (substr($name, 0, 5) == 'HTTP_'){
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        return $headers;
    }
}
new index();
